package com.webside.points.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.webside.points.mapper.IssueMapper;
import com.webside.points.model.IssueEntity;
import com.webside.points.service.IssueService;
import com.webside.points.wechat.BeanFactory;
import com.webside.points.wechat.Util;
@Service("issueService")
public class IssueServiceImpl implements IssueService{

	@Autowired
	IssueMapper issueMapper;
	
	public static IssueService getInstance(){
		return (IssueService) BeanFactory.getBean("issueService");
    }
	
	
	@Override
	public int insert(IssueEntity issueEntity) {
		//base64加密
		issueEntity.setIssueContent(Util.getBase64(issueEntity.getIssueContent()));
		return issueMapper.insert(issueEntity);
	}

	@Override
	public List<IssueEntity> queryAnswer(Map<String, Integer> map) {
		List<IssueEntity> list=issueMapper.queryAnswer(map);
		
		if(list!=null){
			for(int i=0;i<list.size();i++){
				IssueEntity issueEntity=list.get(i);
				//base64解密
				issueEntity.setIssueContent(Util.getFromBase64(issueEntity.getIssueContent()));
				list.set(i, issueEntity);
			}
		}
		
		return list;
	}

	@Override
	public List<IssueEntity> findRecord(Map<String, Integer> map) {
		List<IssueEntity> list=issueMapper.findRecord(map);
		if(list!=null){
			for(int i=0;i<list.size();i++){
				IssueEntity issue=list.get(i);
				//base64解密
				issue.setIssueContent(Util.getFromBase64(issue.getIssueContent()));
				list.set(i, issue);
			}
		}
		return list;
	}

	@Override
	public int addIssue(IssueEntity issueEntity) {
		return issueMapper.addIssue(issueEntity);
	}

	@Override
	@Cacheable(value="ehcache")
	public List<IssueEntity> findAll(IssueEntity issueEntity) {
		List<IssueEntity> list=issueMapper.findAll(issueEntity);
		if(list!=null){
			for(int i=0;i<list.size();i++){
				IssueEntity issue=list.get(i);
				//base64解密
				issue.setIssueContent(Util.getFromBase64(issue.getIssueContent()));
				list.set(i, issue);
			}
		}
		
		return list;
	}

	@Override
	@Cacheable(value="ehcache")
	public int findCount(IssueEntity issueEntity) {
		return issueMapper.findCount(issueEntity);
	}

	@Override
	public IssueEntity findById(int issueId) {
		IssueEntity issueEntity = issueMapper.findById(issueId);
		if(issueEntity!=null){
			issueEntity.setIssueContent(Util.getFromBase64(issueEntity.getIssueContent()));
		}
		
		return issueEntity;
	}

	@Override
	public int updateStatus(IssueEntity issueEntity) {
		return issueMapper.updateStatus(issueEntity);
	}


	@Override
	public List<IssueEntity> findUserIssue(Map<String, Integer> map) {
		List<IssueEntity> list = issueMapper.findUserIssue(map);
		if(list!=null){
			for(int i=0;i<list.size();i++){
				IssueEntity issueEntity = list.get(i);
				issueEntity.setIssueContent(Util.getFromBase64(issueEntity.getIssueContent()));
				list.set(i, issueEntity);
			}
		}
		
		return list;
	}

	@Override
	public List<IssueEntity> issueSearch(Map<String,Object> map) {
		
		List<IssueEntity> list = issueMapper.issueSearch(map);
		if(list!=null){
			for(int i=0;i<list.size();i++){
				IssueEntity issueEntity = list.get(i);
				issueEntity.setIssueContent(Util.getFromBase64(issueEntity.getIssueContent()));
				list.set(i, issueEntity);
			}
		}
		
		return list;
	}
	

}
